package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.HaomakuConfig;
import com.ruoyi.system.service.IHaomakuConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 系统配置Controller
 *
 * @author hjf
 * @date 2025-03-12
 */
@RestController
@RequestMapping("/hmk/systemConfig")
public class HaomakuConfigController extends BaseController {
    @Autowired
    private IHaomakuConfigService haomakuConfigService;

    /**
     * 查询系统配置列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:systemConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(HaomakuConfig haomakuConfig) {
        startPage();
        List<HaomakuConfig> list = haomakuConfigService.selectHaomakuConfigList(haomakuConfig);
        return getDataTable(list);
    }

    /**
     * 导出系统配置列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:systemConfig:export')")
    ////@Log(title = "系统配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HaomakuConfig haomakuConfig) {
        List<HaomakuConfig> list = haomakuConfigService.selectHaomakuConfigList(haomakuConfig);
        ExcelUtil<HaomakuConfig> util = new ExcelUtil<HaomakuConfig>(HaomakuConfig.class);
        util.exportExcel(response, list, "系统配置数据");
    }


    /**
     * 获取系统配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('hmk:systemConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(haomakuConfigService.selectHaomakuConfigById(id));
    }


    @PreAuthorize("@ss.hasPermi('hmk:systemConfig:getLoserCauseData')")
    @GetMapping(value = "/getLoserCauseData")
    public AjaxResult getLoserCauseData() {
        HaomakuConfig haomakuConfig = haomakuConfigService.selectHaomakuConfigById(1L);
        String orderFailType = haomakuConfig.getOrderFailType();
        String[] strings = orderFailType.split("\\|");
        HashMap<Integer, String> map = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            map.put(i, strings[i]);
        }
        return success(map);
    }


    @PermitAll
    @GetMapping(value = "/getCancellationCauseData")
    public AjaxResult getCancellationCauseData() {
        HaomakuConfig haomakuConfig = haomakuConfigService.selectHaomakuConfigById(1L);
        String orderCancelType = haomakuConfig.getOrderCancelType();
        String[] strings = orderCancelType.split("\\|");
        HashMap<Integer, String> map = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            map.put(i, strings[i]);
        }
        return success(map);
    }


    /**
     * 新增系统配置
     */
    @PreAuthorize("@ss.hasPermi('hmk:systemConfig:add')")
    //@Log(title = "系统配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HaomakuConfig haomakuConfig) {
        return toAjax(haomakuConfigService.insertHaomakuConfig(haomakuConfig));
    }

    /**
     * 修改系统配置
     */
    @PreAuthorize("@ss.hasPermi('hmk:systemConfig:edit')")
    //@Log(title = "系统配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HaomakuConfig haomakuConfig) {
        return toAjax(haomakuConfigService.updateHaomakuConfig(haomakuConfig));
    }

    /**
     * 删除系统配置
     */
    @PreAuthorize("@ss.hasPermi('hmk:systemConfig:remove')")
    //@Log(title = "系统配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(haomakuConfigService.deleteHaomakuConfigByIds(ids));
    }


}
